﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtBusiness.OnLine;
using System.Data;
using ExtBusiness;
using System.Web.Security;
using System.Drawing;
using ExtBusiness.FlowOperation;
using System.Configuration;

public partial class office_MyShenHe : PageBase
{
    private RequestFlowOperation rfoGloab = new RequestFlowOperation();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlTjlb.DataBind();
            BindFlowName();
            BindMyHandled();            
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }

    public DataTable GetDataSource()
    {
        string strWhere = string.Empty;
        if (!string.IsNullOrEmpty(txtIID.Text))//受理编号
            strWhere += string.Format(" and slbh like '%{0}%'", txtIID.Text.Trim());
        if (!string.IsNullOrEmpty(txtName.Text.Trim()))//申请单位
            strWhere += string.Format(" and t.单位名称 like '%{0}%'", txtName.Text.Trim());
        if (!string.IsNullOrEmpty(ddlFlow.SelectedItem.Value))//流程名称
            strWhere += string.Format(" and m.案件类型 = '{0}'", ddlFlow.SelectedItem.Value);

        if (!string.IsNullOrEmpty(slstartDate.Value))//受理开始时间
            strWhere += string.Format(" and m.slsj >=to_date('{0}','yyyy-mm-dd')", slstartDate.Value);
        if (!string.IsNullOrEmpty(slendDate.Value))//受理结束时间
            strWhere += string.Format(" and m.slsj <= to_date('{0}','yyyy-mm-dd')", slendDate.Value);

        if (!string.IsNullOrEmpty(jjstartDate.Value))//岗位接件开始时间
            strWhere += string.Format(" and m.接收时间 >=to_date('{0}','yyyy-mm-dd')", jjstartDate.Value);
        if (!string.IsNullOrEmpty(jjendDate.Value))//岗位接件结束时间
            strWhere += string.Format(" and m.接收时间 <= to_date('{0}','yyyy-mm-dd')", jjendDate.Value);

       
        if (!string.IsNullOrEmpty(ddlTjlb.Text))//图件类别
            strWhere += string.Format(" and x.dtyt='{0}'", ddlTjlb.Text);
        if (!string.IsNullOrEmpty(txtShr.Text))//审核人
            strWhere += string.Format(" and exists (select 编号 from sv_work_item_list where 经办人 like '%{0}%' and 编号=m.顺序号)", txtShr.Text);
        if (!string.IsNullOrEmpty(txtTm.Text))//图名
            strWhere += string.Format(" and  t.图名 like '%{0}%'", txtTm.Text);

        if (cbxCss.Checked)//曾送审
        {
            strWhere += string.Format(" and x.IS_CSS='1'");
        }

        //拓展表业务状态(0:已受理;2:已办结;-1:返回补证;-2:驳回中止;-3:不予受理;-4:审批不通过)
        DataTable dtTemp = new DataTable();

        dtTemp = rfoGloab.GetMyShenPiByUserID(UserId, strWhere);

        return dtTemp;
    }

    /// <summary>
    /// 绑定留言咨询信息
    /// </summary>
    /// <!--addby zhongjian 20091103-->
    protected void BindMyHandled()
    {
        //拓展表业务状态(0:已受理;2:已办结;-1:返回补证;-2:驳回中止;-3:不予受理;-4:审批不通过)
        DataTable dtTemp=GetDataSource();

        string[] arrDtsSteps =  ConfigurationManager.AppSettings["dtsSteps"].Split(',');

        if( Request["flag"]=="2")//代表窗口受理人员
        {
            string[] arrJszxSteps = ConfigurationManager.AppSettings["jssczxSteps"].Split(',');

            foreach( DataRow drTemp in dtTemp.Rows)
            {
                foreach (string strTemp in arrJszxSteps)
                {
                    if (drTemp["step"].ToString() == strTemp)
                    {
                        drTemp["step"] = "技术审查";
                    }
                }

                foreach (string strTemp in arrDtsSteps)
                {
                    if (drTemp["step"].ToString() == strTemp)
                    {
                        drTemp["step"] = "行政审批";
                    }
                }
            }
        }
        else if( Request["flag"]=="3")//代表技术审查中心人员
        {
            gvMessage.Columns[15].Visible = true;//退改
            gvMessage.Columns[16].Visible = true;//归档
            //gvMessage.Columns[16].Visible = true;//曾送审

            foreach( DataRow drTemp in dtTemp.Rows)
            {
                foreach (string strTemp in arrDtsSteps)
                {
                    if (drTemp["step"].ToString() == strTemp)
                    {
                        drTemp["step"] = "行政审批";
                    }
                }
            }
        }
        
        this.gvMessage.DataSource = dtTemp;
        this.gvMessage.RecordCount = dtTemp.Rows.Count;
        this.gvMessage.DataBind();
    }

    /// <summary>
    /// datagrid数据加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvMessage_OnLoadData(object sender, EventArgs e)
    {
        this.BindMyHandled();
    }

    protected void btnRefer_Click(object sender, EventArgs e)
    {
        BindMyHandled();
    }

    /// <summary>
    /// 绑定该用户有权限的流程名称
    /// </summary>
    /// <!--addby zhongjian 20100608-->
    public void BindFlowName()
    {
        ClsWorkFlow newWorkflow = new ClsWorkFlow();
        DataTable dtTable = newWorkflow.GetWorkFlowByUserId(UserId, "");
        ddlFlow.Items.Clear();
        ddlFlow.Items.Add(new ListItem("所有流程", ""));
        foreach (DataRow row in dtTable.Rows)
        {
            ddlFlow.Items.Add(new ListItem(row["wname"].ToString(), row["wname"].ToString()));
        }
    }

    /// <summary>
    /// 行创建
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvMessage_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView dtr = (DataRowView)e.Row.DataItem;

            if (dtr != null)
            {
                HyperLink hlRequestUpdate = e.Row.FindControl("hlRequestUpdate") as HyperLink;

                HyperLink hlTg = e.Row.FindControl("hlTg") as HyperLink;
                HyperLink hlGd = e.Row.FindControl("hlGd") as HyperLink;
              
                //所有的变更业务只针对地图审核流程
                if (ClsWorkFlow.IsHaveSpecialWord(dtr["wname"].ToString()))
                {
                    string strScript = string.Empty;
                    //申请修改
                    strScript = string.Format("SetUpdateNotes('{0}')", dtr["iid"]);
                    hlRequestUpdate.Attributes.Add("onclick", strScript);
                    hlRequestUpdate.NavigateUrl = "#";

                    //针对从外网办理大厅走申报的情况
                    //if (dtr["sjly"].ToString() == "网上")
                    //{
                    //    int nBg = Convert.ToInt32(dtr["is_bg"]);
                    //    //变更申请
                    //    if (nBg == 0)
                    //    {
                    //        strScript = string.Format("SetSubmitFlag('{0}','is_bg')", dtr["iid"]);
                    //        hlBgsq.Attributes.Add("onclick", strScript);
                    //        hlBgsq.NavigateUrl = "#";
                    //    }
                    //    else
                    //    {
                    //        hlBgsq.BackColor = Color.Silver;
                    //        hlBgsq.Enabled = false;
                    //    }
                    //    //延期申请
                    //    int nYq = Convert.ToInt32(dtr["is_yq"]);
                    //    if (nYq == 0)
                    //    {
                    //        strScript = string.Format("SetSubmitFlag('{0}','is_yq')", dtr["iid"]);
                    //        hlYqsq.Attributes.Add("onclick", strScript);
                    //        hlYqsq.NavigateUrl = "#";
                    //    }
                    //    else
                    //    {
                    //        hlYqsq.BackColor = Color.Silver;
                    //        hlYqsq.Enabled = false;
                    //    }
                    //    //撤销申请
                    //    int nCx = Convert.ToInt32(dtr["is_cx"]);
                    //    if (nCx == 0)
                    //    {
                    //        strScript = string.Format("SetSubmitFlag('{0}','is_cx')", dtr["iid"]);
                    //        hlCxsq.Attributes.Add("onclick", strScript);
                    //        hlCxsq.NavigateUrl = "#";
                    //    }
                    //    else
                    //    {
                    //        hlCxsq.BackColor = Color.Silver;
                    //        hlCxsq.Enabled = false;
                    //    }
                    //}
                    //else//直接从内网走的审批流程
                    //{
                    //    hlBgsq.Visible = false;
                    //    hlYqsq.Visible = false;
                    //    hlCxsq.Visible = false;
                    //}

                    //退改
                    int nTg = Convert.ToInt32(dtr["is_Tg"]);
                    if (nTg == 1)
                    {
                        hlTg.BackColor = Color.Silver;
                    }
                    strScript = string.Format("recordFlag('{0}','is_tg',{1})", dtr["iid"], nTg);
                    hlTg.Attributes.Add("onclick", strScript);
                    hlTg.NavigateUrl = "#";

                    //归档
                    int nGd = Convert.ToInt32(dtr["is_gd"]);
                    if (nGd == 1)
                    {
                        hlGd.BackColor = Color.Silver;
                    }
                    strScript = string.Format("recordFlag('{0}','is_gd',{1})", dtr["iid"], nGd);
                    hlGd.Attributes.Add("onclick", strScript);
                    hlGd.NavigateUrl = "#";
                }
                else//隐藏按钮
                {
                    hlRequestUpdate.Visible = false;
                    hlTg.Visible = false;
                    hlGd.Visible = false;
                }

            }
        }
    }

    /// <summary>
    /// excel导出事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvMessage_OnImportClick(object sender, EventArgs e)
    {
        DataTable dtTemp = GetDataSource();

        dtTemp.Columns["iid"].ColumnName = "办件编号";

        dtTemp.Columns["wname"].ColumnName = "办件类型";
        dtTemp.Columns["step"].ColumnName = "所在岗位";
        dtTemp.Columns["sjly"].ColumnName = "数据来源";
        dtTemp.Columns["tm"].ColumnName = "图名";
        dtTemp.Columns["slh"].ColumnName = "受理编号";
        dtTemp.Columns["name"].ColumnName = "申请单位";
        dtTemp.Columns["accepted_time"].ColumnName = "受理时间";
        dtTemp.Columns["dtyt"].ColumnName = "图件用途";
        dtTemp.Columns["is_tg"].ColumnName = "是否退改";
        dtTemp.Columns["is_gd"].ColumnName = "是否归档";

        if (dtTemp.Columns.Contains("PRIORITY"))
        {
            dtTemp.Columns.Remove("PRIORITY");
        }
        if (dtTemp.Columns.Contains("STATUS"))
        {
            dtTemp.Columns.Remove("STATUS");
        }

        if (!string.IsNullOrEmpty(txtShr.Text))
        {
            dtTemp.Columns.Add("审查人");
            foreach (DataRow drTemp in dtTemp.Rows)
            {
                drTemp["审查人"] = txtShr.Text;
            }
        }

        Common.ExcelOperation.TableToExcelCustom(dtTemp, Guid.NewGuid().ToString());
    }
}
